package com.itzhigui.controller;

import com.itzhigui.task.MyTask2;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.concurrent.Future;

/**
 * 测试异步任务的使用
 *
 * @author java1412
 * @since 2024-04-15 15:48:56
 */
@RestController
public class TestTaskController {

    @Resource
    private MyTask2 myTask2;

    @GetMapping(value = "/testTask")
    public String testTask() {
        long start = System.currentTimeMillis();
        Future<String> t1 = myTask2.task1();
        Future<String> t2 = myTask2.task2();
        Future<String> t3 = myTask2.task3();
        while (true) {
            // 判断三个任务执行完成，结束死循环
            if (t1.isDone() && t2.isDone() && t3.isDone()) {
                break;
            }
        }
        long end = System.currentTimeMillis();
        System.out.println("总耗时:" + (end - start) / 1000.0 + "秒");
        return "ok";
    }
}
